//
//  NSString+XM.m
//  XMSimonUtils
//
//  Created by 徐盟 on 2023/7/31.
//  Copyright © 2023 xumeng. All rights reserved.
//

#import "NSString+XM.h"
#import "XM_Define.h"

@implementation NSString (XM)


- (BOOL) isNotEmpty {
    if (![self isEqualToString:@""]) {
        return YES;
    }
    if ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] > 0) {
        return YES;
    }
    return NO;
}



- (NSString *) trim {
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (NSString *) trimAString:(NSString *)aString
{
    NSString * str = [self leftTrimAString:aString];
    return  [str rightTrimAString:aString];
}

- (NSString *) leftTrimAString:(NSString * _Nullable)aString
{
    NSInteger idx = 0;
    NSInteger length = self.length;
    for (NSInteger i=0; i<length; i++) {
        NSString * str = [self substringWithRange:NSMakeRange(i, 1)];
        if([str isEqualToString:aString]){
            idx = i+1;
        }else{
            break;
        }
    }
    return [self substringFromIndex:idx];
}

- (NSString *) rightTrimAString:(NSString * _Nullable)aString
{
    NSInteger length = self.length;
    NSInteger idx = length;

    for (NSInteger i=length-1; i>-1; i--) {
        NSString * str = [self substringWithRange:NSMakeRange(i, 1)];
        if([str isEqualToString:aString]){
            idx = i;
        }else{
            break;
        }
    }
    return [self substringToIndex:idx];
}
- (NSString *) replaceAll:(NSString*)origin with:(NSString*)replacement {
    return [self stringByReplacingOccurrencesOfString:origin withString:replacement];
}

- (NSArray *) split:(NSString*) separator {
    return [self componentsSeparatedByString:separator];
}

- (NSString *) toLowerCase {
    return [self lowercaseString];
}

- (NSString *) toUpperCase {
    return [self uppercaseString];
}


- (NSString *) substringFromIndex:(int)beginIndex toIndex:(int)endIndex {
    if (!self.isNotEmpty) {
        return @"";
    }
    if (beginIndex >= endIndex) {
        return @"";
    }
    NSRange range = NSMakeRange(beginIndex, endIndex - beginIndex);
    return [self substringWithRange:range];
}


- (CGSize)sizeWithFont:(CGFloat)fontSize
{
    CGSize maxSize = CGSizeMake(XM_ScreenWidth, fontSize*2);
    return [self sizeWithFont:fontSize maxSize:maxSize];
}

- (CGSize)sizeWithFont:(CGFloat)fontSize maxSize:(CGSize)maxSize
{
    UIFont * font = [UIFont systemFontOfSize:fontSize];
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

- (CGSize)sizeWithFont:(CGFloat)fontSize maxSize:(CGSize)maxSize andlineSpacing:(CGFloat) lineSpaceing
{
    UIFont * font = [UIFont systemFontOfSize:fontSize];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.hyphenationFactor = 1.0;
    paragraphStyle.lineSpacing = lineSpaceing;
    paragraphStyle.firstLineHeadIndent = 0.0;
    paragraphStyle.paragraphSpacingBefore = 0.0;
    paragraphStyle.headIndent = 0;
    paragraphStyle.tailIndent = 0;
    NSDictionary *dict = @{NSFontAttributeName : font,NSParagraphStyleAttributeName:paragraphStyle.copy};
    
    CGSize originSize = [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    
    NSString *test = @"我们";
    CGSize wordSize = [test sizeWithFont:fontSize maxSize:maxSize];
    
    CGFloat selfHeight = [self sizeWithFont:fontSize maxSize:maxSize].height;
    
    if (selfHeight <= wordSize.height) {
        
        CGSize newSize = CGSizeMake(originSize.width, font.pointSize);
        
        if (selfHeight == 0) {
            
            newSize = CGSizeMake(originSize.width, 0);
        }
        
        originSize = newSize;
    }
    
    return originSize;
}

@end
